; Test file for R4RS Section 6.10 Input and Output procedures

(display "Testing basic I/O procedures according to R4RS Section 6.10\n")
(newline)

; Test write procedure
(display "Testing write procedure:\n")
(write 42)
(display "\n")
(write "Hello, world!")
(display "\n")
(write #t)
(display "\n")
(write '(1 2 3))
(display "\n\n")

; Test display procedure
(display "Testing display procedure:\n")
(display 42)
(display "\n")
(display "Hello, world!")
(display "\n")
(display #t)
(display "\n")
(display '(1 2 3))
(display "\n\n")

; Test write-char procedure
(display "Testing write-char procedure:\n")
(write-char #\H)
(write-char #\e)
(write-char #\l)
(write-char #\l)
(write-char #\o)
(write-char #\,)
(write-char #\space)
(write-char #\w)
(write-char #\o)
(write-char #\r)
(write-char #\l)
(write-char #\d)
(write-char #\!)
(newline)
(newline)

; Test read, read-char and eof-object? procedures
; Note: These tests are interactive and require user input
(display "Testing interactive I/O procedures:\n")
(display "Type a character and press Enter: ")
(define input-char (read-char))
(display "You entered: ")
(write-char input-char)
(newline)
(display "Check if it's EOF: ")
(write (eof-object? input-char))
(newline)
(newline)

(display "Type a simple expression (e.g., (+ 1 2)) and press Enter: ")
(define input-expr (read))
(display "You entered the expression: ")
(write input-expr)
(display "\nResult of evaluating it: ")
(write (eval input-expr (interaction-environment)))
(newline)
(newline)

; Demonstrate a more complex I/O operation
(display "Demonstrating a text processing example:\n")
(display "Type a line of text and press Enter: ")
(define (read-line) 
  (define (read-chars) 
    (let ((c (read-char))) 
      (if (or (eof-object? c) (char=? c #\newline)) 
          '() 
          (cons c (read-chars))))) 
  (list->string (read-chars)))

(define input-line (read-line))
(display "You entered: ")
(display input-line)
(newline)
(display "Length of input: ")
(write (string-length input-line))
(newline)
(display "Input in uppercase: ")
(define (string-upcase s) 
  (list->string 
    (map (lambda (c) 
           (if (char-lower-case? c) 
               (char-upcase c) 
               c)) 
         (string->list s))))
(display (string-upcase input-line))
(newline)
(newline)

(display "All I/O procedure tests completed successfully!\n")